package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.DataActivity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;
import com.example.win.android_banduyujia.peopleactivity.ui.mapView.MyApplication;
import com.example.win.android_banduyujia.peopleactivity.ui.mapView.MyIcon;
import com.example.win.android_banduyujia.peopleactivity.ui.mapView.MyIcon2;
import com.example.win.android_banduyujia.peopleactivity.ui.mapView.MyMapView;

/**
 * Created by Win on 2015/9/6.
 */
public class TeachAddressActivity extends BaseTitleBarActivity {
    public static final String  TAG = TeachAddressActivity.class.getSimpleName();

    public MyApplication app;
    static MyMapView mMapView = null;

    public MKMapViewListener mMapListener = null;
    MyLocationOverlay myLocationOverlay = null;
    // 定位相关
    LocationClient mLocClient;
    public NotifyLister mNotifyer = null;
    public MyLocationListenner myListener = new MyLocationListenner();
    LocationData locData = null;
    private MapController mMapController = null;

    static MKSearch mkSerach;

    Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(TeachAddressActivity.this, "msg:" + msg.what,
                    Toast.LENGTH_SHORT).show();
        };
    };
    static TextView showAddr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_address_page);

        showAddr = (TextView)findViewById(R.id.text_view_address);
        MyIcon   mi =  new MyIcon(this);
        //在屏幕中心点添加接我图标
        getWindow().addContentView(mi, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        MyIcon2 m2 = new MyIcon2(this);
        getWindow().addContentView(m2, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        mMapView=(MyMapView)findViewById(R.id.map_view);
        mMapController = mMapView.getController();

        initMapView();
        app = MyApplication.getInstance();
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);

        //搜索初始化
        mkSerach = new MKSearch();
        mkSerach.init(app.mBMapManager, new MKSearchListener() {
            @Override
            public void onGetPoiResult(MKPoiResult mkPoiResult, int i, int i1) {

            }

            @Override
            public void onGetTransitRouteResult(MKTransitRouteResult mkTransitRouteResult, int i) {

            }

            @Override
            public void onGetDrivingRouteResult(MKDrivingRouteResult mkDrivingRouteResult, int i) {

            }

            @Override
            public void onGetWalkingRouteResult(MKWalkingRouteResult mkWalkingRouteResult, int i) {

            }

            @Override
            public void onGetAddrResult(MKAddrInfo info, int i) {
                showAddr.setText(info.strAddr);
            }

            @Override
            public void onGetBusDetailResult(MKBusLineResult mkBusLineResult, int i) {

            }

            @Override
            public void onGetSuggestionResult(MKSuggestionResult mkSuggestionResult, int i) {

            }

            @Override
            public void onGetPoiDetailSearchResult(int i, int i1) {

            }
        });
        //设置地图相关
        LocationClientOption  option = new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd09ll");
        option.setScanSpan(300000);
        mLocClient.setLocOption(option);
        mLocClient.start();
        mMapView.getController().setZoom(16);
        mMapView.getController().enableClick(true);
        mMapView.displayZoomControls(true);
        mMapListener = new MKMapViewListener(){

            @Override
            public void onMapMoveFinish() {

            }

            @Override
            public void onClickMapPoi(MapPoi mapPoi) {
                String  title = " ";
                if(mapPoi !=null){
                    title = mapPoi.strText;
                    Toast.makeText(TeachAddressActivity.this, title, Toast.LENGTH_SHORT).show();
                }
            }
        };
        mMapView.regMapViewListener(MyApplication.getInstance().mBMapManager, mMapListener);
        myLocationOverlay = new MyLocationOverlay(mMapView);
        locData = new LocationData();
        myLocationOverlay.setData(locData);
        mMapView.getOverlays().add(myLocationOverlay);
        myLocationOverlay.enableCompass();
        mMapView.refresh();

        InitTitles();
        InitViews();
    }

    private void InitTitles() {
        showLeftBackButton();
        setTitle(R.string.layout_data_page_text_view_address);
    }

    private void InitViews() {


    }

    private void initMapView() {
        mMapView.setLongClickable(true);
    }

    /**
     * 监听函数，又新位置的时候，格式化成字符串，输出到屏幕中
     */
    public class MyLocationListenner implements BDLocationListener{

        @Override
        public void onReceiveLocation(BDLocation location) {
            Log.i("================", " null ============ null" + location);

            if (location == null)
                return;
            Log.i("================", "not null ============not null");
            // 31.192695,121.42712,3000
            locData.latitude = location.getLatitude();
            locData.longitude = location.getLongitude();
            locData.direction = 2.0f;
            locData.accuracy = location.getRadius();
            locData.direction = location.getDerect();
            Log.d("loctest",
                    String.format("before: lat: %f lon: %f",
                            location.getLatitude(), location.getLongitude()));
            // GeoPoint p = CoordinateConver.fromGcjToBaidu(new
            // GeoPoint((int)(locData.latitude* 1e6), (int)(locData.longitude *
            // 1e6)));
            // Log.d("loctest",String.format("before: lat: %d lon: %d",
            // p.getLatitudeE6(),p.getLongitudeE6()));
            myLocationOverlay.setData(locData);
            mMapView.refresh();

            // mMapController.setCenter(new GeoPoint((int) (locData.latitude *
            // 1e6),
            // (int) (locData.longitude * 1e6)));
            mMapController
                    .animateTo(new GeoPoint((int) (locData.latitude * 1e6),
                            (int) (locData.longitude * 1e6)), mHandler
                            .obtainMessage(1));
        }

        @Override
        public void onReceivePoi(BDLocation bdLocation) {
            if(bdLocation == null){
                return;
            }
        }
    }

    public class NotifyLister extends BDNotifyListener {
        public void onNotify(BDLocation mlocation, float distance) {
        }
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mMapView.onRestoreInstanceState(savedInstanceState);
    }

    public static void getPosition(GeoPoint g) {
        mkSerach.reverseGeocode(g);
        showAddr.setText("获取位置中...");
    }
}
